package org.mspring.mlog.service.bbs.impl;

import java.util.Date;

import org.mspring.mlog.entity.bbs.Topic;
import org.mspring.mlog.service.bbs.TopicReplyService;
import org.mspring.mlog.service.bbs.TopicService;
import org.mspring.mlog.web.security.SecurityUtils;
import org.mspring.platform.dao.hibernate.GenericDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author Gao Youbo
 * @since 2013年8月6日
 */
@Service
@Transactional
public class TopicServiceImpl extends GenericDao<Topic, Long> implements TopicService {

    @Autowired
    private TopicReplyService topicReplyService;

    @Override
    public Topic createTopic(Topic topic) {
        // TODO Auto-generated method stub
        if (topic.getCreateTime() == null) {
            topic.setCreateTime(new Date());
        }
        if (topic.getAuthor() == null) {
            topic.setAuthor(SecurityUtils.getCurrentUser());
        }
        return super.save(topic);
    }

    @Override
    public void deleteTopic(Long... id) {
        // TODO Auto-generated method stub
        if (id == null || id.length == 0) {
            return;
        }
        for (Long i : id) {
            // 先删除回帖
            topicReplyService.deleteReplyByTopic(i);
            // 再删除帖子
            super.remove(i);
        }
    }

    @Override
    public void updateTopic(Topic topic) {
        // TODO Auto-generated method stub
        if (topic.getCreateTime() == null) {
            topic.setCreateTime(new Date());
        }
        if (topic.getAuthor() == null) {
            topic.setAuthor(SecurityUtils.getCurrentUser());
        }
        topic.setModifyTime(new Date());
        super.update(topic);
    }
}
